package com.aiyige.page.player;

import android.os.Bundle;
import android.widget.RelativeLayout;

import com.aiyige.R;
import com.aiyige.arouter.config.ARouterConfig;
import com.aiyige.base.BaseActivity;
import com.aiyige.page.my.download.util.DownloadUtil;
import com.aiyige.utils.ToastX;
import com.aiyige.utils.UIHelper;
import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 全屏播放器，支持本地和在线
 * Created by xiachao on 2017/12/29.
 */
@Route(path = ARouterConfig.PlayerPage)
public class PlayerPage extends BaseActivity implements PlayerView.PlayCallback {


    @BindView(R.id.player)
    PlayerView player;
    @BindView(R.id.rl_player)
    RelativeLayout rlPlayer;

    @Autowired
    Bundle bundle;
    @Autowired
    String modelId;
    @Autowired
    int fileId;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.page_player);
        ButterKnife.bind(this);
        ARouter.getInstance().inject(this);
        if (getIntent().getSerializableExtra("playList") != null) {
            List<PlayerInfo> playList = (ArrayList) getIntent().getSerializableExtra("playList");
            if (playList == null || playList.isEmpty()) {
                ToastX.show(getString(R.string.playlist_is_empty));
                finish();
                return;
            } else {
                player.setPlayCallback(this);
                player.orientationChange(true);
                player.start(playList, 0);
            }
        } else {
            ToastX.show(getString(R.string.playlist_is_empty));
            finish();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        UIHelper.hideSystemUI(this);
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (player != null) {
            DownloadUtil.recordPlayHistory(modelId, fileId, player.getLastPlayPosition());
        }
    }

    @Override
    public void playChange(int position) {

    }

    @Override
    public void buy() {

    }

    @Override
    public void orientation(boolean landscape) {

    }

    @Override
    public void downloadVideos() {

    }

    @Override
    public void onPlaying() {
        if (player != null) {// 修复未开始播放之前的竖屏 按钮摆放bug
            player.orientationChange(player.isLandspace);
        }
    }

    @Override
    public void onComplete() {

    }

    @Override
    public void onError(int errorCode, String errorDetail) {

    }
}
